/**
 * 
 * @author Michael Serajnik
 */

package at.fhj.itm10.ticketmaster.beans;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;

import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;

import org.primefaces.model.UploadedFile;

import at.fhj.itm10.ticketmaster.dao.AddressDAO;
import at.fhj.itm10.ticketmaster.dao.DAOException;
import at.fhj.itm10.ticketmaster.dao.DAOFactory;
import at.fhj.itm10.ticketmaster.dao.EventDAO;
import at.fhj.itm10.ticketmaster.dao.impl.DAOFactoryImpl;
import at.fhj.itm10.ticketmaster.models.Address;
import at.fhj.itm10.ticketmaster.models.Event;

// ACHTUNG: BEAN WIRD NICHT VERWENDET UND DIENT NUR ALS BEISPIEL. CODE WIRD IN EventBean ÜBERFÜHRT!

@ManagedBean
@SessionScoped
public class EventAddingBean {
	
	// Event + EventDAO
	
	private Event event;
	public Event getEvent() {
		return event;
	}
	public void setEvent(Event event) {
		this.event = event;
	}

	private EventDAO eventDao;
	public EventDAO getEventDao() {
		return eventDao;
	}
	public void setEventDao(EventDAO eventDao) {
		this.eventDao = eventDao;
	}
	
	// Address + AddressDAO

	private Address address;
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}

	private AddressDAO addressDao;
	public AddressDAO getAddressDao() {
		return addressDao;
	}
	public void setAddressDao(AddressDAO addressDao) {
		this.addressDao = addressDao;
	}
	
	// Eventtyp (zur Bestimmung des Foreign Keys beim Event durch die Auswahl im Formular)
	
	private String eventType;
	public String getEventType() {
		return eventType;
	}
	public void setEventType(String eventType) {
		this.eventType = eventType;
	}
	
	// Flyer
	
	private UploadedFile image;
	public UploadedFile getImage() {
		return image;
	}
	public void setImage(UploadedFile image) {
		this.image = image;
	}
	
	// Constructor
	
	public EventAddingBean() {
		
		this.init();
		
	}
	
	// Init
	
	@PostConstruct
	public void init() {
		
		try {
			
			DAOFactory daoFactoryImpl = new DAOFactoryImpl();
			setEventDao(daoFactoryImpl.getEventDAO());
			setAddressDao(daoFactoryImpl.getAddressDAO());
			
		} catch(DAOException e) {
			e.printStackTrace();
		}
		
	}
	
	/** 
	 * TODO:
	 * 
	 * - Autocomplete bei Formularfeldern (Adresse)
	 * - Flyer hochladen (m�glicherweise auslagern)
	 * - Formulareingaben verarbeiten
	 * - Foreign Keys anlegen
	 * 		- Session -> fkCustomerId
	 * 		- Autocomplete bei Adresse -> fkAddressId (ggbfs. neue Adresse angelegen)
	 * 		- Dropdownauswahl von Eventtyp -> fkEventtypeId
	 *  - An DAO zum Speichern �bergeben
	 *
	 */
	
	public void selectEventType() {
		
		if(eventType.equals("Typ 1")) {
			event.setFkEventtypeId(1);
		}
		else if(eventType.equals("Typ 2")) {
			event.setFkEventtypeId(2);
		}
		else if(eventType.equals("Typ 3")) {
			event.setFkEventtypeId(3);
		}

		// TODO: das ist nur ein Beispiel, muss schlussendlich mit den verfügbaren Eventtypen angeglichen werden
		
	}
	
	public String writeImage() {
		
		File f = new File("path/to/" + image.getFileName()); // TODO: richtigen Pfad eintragen
		
		try {
			
			InputStream fileInputStream = image.getInputstream();
			
			OutputStream fileOutputStream = new FileOutputStream(f);
			
			byte buf[] = new byte[1024];
			int len;
			
			while((len = fileInputStream.read(buf)) > 0) {
				fileOutputStream.write(buf, 0, len);
			}
			
			fileOutputStream.close();
			fileInputStream.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return f.getAbsolutePath();
		
	}
	
	public void setFlyerUrl(String path) {
		
		event.setFlyer(path);
		
	}
	
	public void insertNewAddress(Address address) {
		
		try {
			addressDao.insert(address);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// TODO: Nach dem Insert die ID der neuen Adresse auslesen und dem Foreign Key beim Event zuweisen
	
	}
	
	public void insertNewEvent(Event event) {
		  
		try {
			eventDao.insert(event);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void saveNewEvent() {
		
		
		
	}
	
}
